<template>
	<view class="login" :style="{height:wh+'px'}">
		<!-- 显示上方的图片 -->
		<view class="title">
			<image src="../../static/icon/login.png" mode="aspectFit"></image>
			<image src="../../static/icon/ps.png" mode="aspectFit"></image>
		</view>
		<!-- 下方的表单 -->
		<view class="content">
			<uni-forms ref="logForm" :model="logFormData" :rules="rules" validateTrigger="blur">
				<uni-forms-item name="username">
					<uni-easyinput v-model="logFormData.username" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput type="password" v-model="logFormData.password" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="login(logForm)">登录</button>
		</view>
		<!-- 下方的跳转注册 -->
		<view class="register">
			<navigator url="/pages/register/register">还没有账号 ，点击注册</navigator>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue'
	// 导入和定义状态库
	import { userStore } from '../../store/userStore';
	import {
		CONFIG
	} from '../../utils/config'
	let store = userStore()
	let wh = computed(() => {
		let h = uni.getWindowInfo().windowHeight
		console.log("h=" + h)
		return h
	})

	let logFormData = ref({
		username: '',
		password: ''
	})

	// 验证规则
	let rules = ref({
		username: {
			rules: [{
					required: true,
					errorMessage: '请输入用户名'
				},
				{
					minLength: 3,
					maxLength: 15,
					errorMessage: '用户必须在{minLength}到{maxLength}个字符之间'
				}, {
					pattern: '^\\w{3,15}$',
					errorMessage: '请输入3-15位数字字母和下划线作为用户名'
				}
			]
		},
		password: {
			rules: [{
				required: true,
				errorMessage: '请输入密码'
			}, {
				pattern: '^[a-zA-Z0-9._!@#$%^&]{3,10}$',
				errorMessage: '请输入3-10位数字字母和特殊字符密码'
			}]
		}
	})


	// 登录
	// 登录表单数据和登录功能
	const logForm = ref(null)

	function login(logForm) {
		logForm.validate().then((res) => {
			let params = encodeURI(`{"username":"${res.username}","password":"${res.password}"}`)
			uni.$get(`${CONFIG.leandcloud_url}/1.1/classes/Customer?where=${params}`)
				.then(res => {

					if (res.results.length > 0) {
						uni.showToast({
							title: "登录成功",
							icon: "success"
						})
						let user = res.results[0]
						console.log(user)
						store.addUserInfo(user.objectId, user.username, user.mobile)
						uni.navigateBack()

					}
				})

		}).catch(err => {
			// 验证失败 
			console.log(err)
		})
	}
</script>

<style>
	.login {
		/* 这里添加边框,页面高度就没问题,如果不添加边框,那么页面内容就多余容器高度,显示滚动条,可以滚动 原因待查，。*/
		border: 1px solid red;
		/* position: relative; */
	}

	/* 图片区域距离上方距离 */
	.login .title {
		margin: 100px 30px 0;


		/* border: 1px solid red; */
	}

	/* 设置图片高度,宽度自适应 */
	.login .title image:nth-child(1) {
		height: 50px;


	}

	.login .title image:nth-child(2) {
		height: 30px;
	}

	/* 内容区域的外边距 */
	.login .content {
		margin: 10px 20px;
	}

	/* 注册链接样式 */
	.login .register {
		position: absolute;
		bottom: 20px;
		width: 100vw;
		text-align: center;
		color: #007aff;
		font-size: 20rpx;
	}
</style>